package com.lcf.app.basic;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.lang.reflect.Method;

@Component(value = "SpringContext")
public class SpringContext implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void myTest() {
        System.out.println("My Test ...");
    }

    @PostConstruct
    public void init() {
        try {
            Object springContext = applicationContext.getBean("SpringContext");
            Class<?> aClass = springContext.getClass();
            Method myTest = aClass.getMethod("myTest");
            myTest.invoke(aClass.newInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
